//
//  QuestionesView.m
//  DeepBreathing
//
//  Created by rimi on 15/12/23.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "QuestionesView.h"
#import "DHCAnimation.h"
#define CLOCK_CENTER CGPointMake(186, 182)
#define CUP_CENTER CGPointMake(182,250)
typedef void(^AnimationBlock)() ;

@interface QuestionesView ()

@property(nonatomic,strong)CALayer *question1Layer;
//第二题
@property(nonatomic,strong)CALayer * clockFace;
@property(nonatomic,strong)CALayer * hourHand;
@property(nonatomic,strong)CALayer * minuteHand;
@property(nonatomic,strong)CALayer * secondHand;
@property(nonatomic,strong)CALayer * question2Text;
//第三题
@property(nonatomic,strong)CALayer * cup;
@property(nonatomic,strong)CALayer * water;
@property(nonatomic,strong)UIImageView * pill;
@property(nonatomic,strong)CALayer * question3Text;
//第四题
@property(nonatomic,strong)CALayer * cross;
@property(nonatomic,strong)CALayer * question4Text;



@property(nonatomic,strong)NSMutableArray *inAnimations;
@property(nonatomic,strong)NSMutableArray *outAnimations;

@end

@implementation QuestionesView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        
        self.currentIndex = 0;
        [self.layer addSublayer:self.question1Layer];
        AnimationBlock block = self.inAnimations[0];
        block();
        
        
    }
    return self;
}

#pragma mark -- privateMethods
-(CAAnimationGroup *)translationandRotationFromPosition:(CGPoint)transPosition toPosition:(NSValue *)toPosition{
    DHCAnimation *translationAnimation  = [DHCAnimation animationWithKeyPath:@"position" duration:0.8 fromValue:[NSValue valueWithCGPoint:transPosition] completionBlock:^(BOOL flag) {
        
    }];
    
    if (toPosition) {
        translationAnimation.animation.toValue  = toPosition;
    }
    translationAnimation.animation.toValue = toPosition;
    DHCAnimation *Ration = [DHCAnimation animationWithKeyPath:@"transform.rotation" duration:0.8 fromValue:@(-4*M_PI) completionBlock:^(BOOL flag) {
        
    }];
    
    
   
    CAAnimationGroup *Aniamtion = [CAAnimationGroup animation];
    Aniamtion.animations = @[translationAnimation.animation,Ration.animation];
    Aniamtion.duration  = 0.8;
    
    return Aniamtion;

    
}
-(void)loadLastQuestion{
    
    AnimationBlock block = self.outAnimations[_currentIndex--];
  
    AnimationBlock nextBlock = self.inAnimations[_currentIndex];
    if (block) {
        block();
    }
    nextBlock();

    
}
-(void)loadNextQuestion{
    
//    void(^block)(void) = self.outAnimations[_currentIndex];
    AnimationBlock block = self.outAnimations[_currentIndex++];

        if (block) {
        block();
    }
    if (_currentIndex == 4) {
        return;
    }
    AnimationBlock nextBlock = self.inAnimations[_currentIndex];

    
    nextBlock();
    
}
#pragma mark -- 动画实现的Block
- (NSMutableArray *)inAnimations
{
    if (!_inAnimations) {
        _inAnimations = [NSMutableArray arrayWithArray:@[[self loadQuestion1:YES],
                                                         [self loadQuestion2:YES],
                                                         [self loadQuestion3:YES],
                                                         [self loadQuestion4:YES]]];
    }
    return _inAnimations;
}

- (NSMutableArray *)outAnimations
{
    if (!_outAnimations) {
        _outAnimations = [NSMutableArray arrayWithArray:@[[self loadQuestion1:NO],
                                                          [self loadQuestion2:NO],
                                                          [self loadQuestion3:NO],
                                                          [self loadQuestion4:NO]]];
    }
    return _outAnimations;
}




- (CALayer *)question1Layer
{
    if (!_question1Layer) {
        _question1Layer = [CALayer layer];
        
        _question1Layer.frame = CGRectMake(112, 68, 170, 250);
        _question1Layer.contents = (__bridge id)[UIImage imageNamed:@"question1.png"].CGImage;
    }
    return _question1Layer;
}
//第四题
-(CALayer *)cross{
    if (!_cross) {
        _cross = [CALayer layer];
        _cross.frame = CGRectMake(0, 0, 135, 135);
        _cross.position = CGPointMake(183, 156);
        _cross.contents = (__bridge id)[UIImage imageNamed:@"十字"].CGImage;
    }
    return _cross;
}
-(CALayer *)question4Text{
    
    
    if (!_question4Text) {
        _question4Text = [CALayer layer];
        _question4Text.frame = CGRectMake(0, 0, 150, 60);
        _question4Text.position = CGPointMake(180, 270);
        _question4Text.contents = (__bridge id)[UIImage imageNamed:@"急性发作"].CGImage;
        
    }
    return _question4Text;
}
//第三题
-(CALayer *)cup{
    
    
    if (!_cup) {
        _cup  = [CALayer layer];
        _cup.frame = CGRectMake(0, 0, 95, 135);
        _cup.position = CUP_CENTER;
        _cup.contents = (__bridge id)[UIImage imageNamed:@"杯子"].CGImage;
    }
    return _cup;
}
-(CALayer *)water{
    
    if (!_water) {
        _water = [CALayer layer];
        _water.frame = CGRectMake(0, 0, 88, 95);
        _water.position = CUP_CENTER;
        _water.contents = (__bridge id)[UIImage imageNamed:@"水"].CGImage;
        
        _water.opacity = 1;
        
    }
    return _water;
    
}
-(UIImageView *)pill{
    
    
    if (!_pill) {
        _pill = [[UIImageView alloc]init];
        _pill.frame = CGRectMake(0, 0, 45, 45);
        _pill.center = CGPointMake(170, - 263);
        _pill.image = [UIImage imageNamed:@"药丸"];
        
    }
    return _pill;
}

-(CALayer *)question3Text{
    
    if (!_question3Text) {
        _question3Text = [CALayer layer];
        _question3Text.frame = CGRectMake(93, 350, 182, 58);
        _question3Text.contents = (__bridge id)[UIImage imageNamed:@"缓解药物"].CGImage;
    }
    
    return _question3Text;
    
}

- (CALayer *)clockFace
{
    if (!_clockFace) {
        _clockFace = [CALayer layer];
        _clockFace.frame = CGRectMake(0, 0, 255, 255);
        _clockFace.position = CLOCK_CENTER;
        _clockFace.contents = (__bridge id)[UIImage imageNamed:@"表盘.png"].CGImage;
    }
    return _clockFace;
}

- (CALayer *)hourHand
{
    if (!_hourHand) {
        _hourHand = ({
            
            CALayer * layer = [CALayer layer];
            layer.frame = CGRectMake(0, 0, 51, 67);
            layer.position = CLOCK_CENTER;
            layer.anchorPoint = CGPointMake(0.9, 0.9);
            layer.contents = (__bridge id)[UIImage imageNamed:@"时钟"].CGImage;
            layer;
            
        });
    }
    return _hourHand;
}

- (CALayer *)minuteHand
{
    if (!_minuteHand) {
        _minuteHand = ({
            
            CALayer * layer = [CALayer layer];
            layer.frame = CGRectMake(0, 0, 95, 8);
            layer.position = CLOCK_CENTER;
            layer.anchorPoint = CGPointMake(0, 0.5);
            layer.contents = (__bridge id)[UIImage imageNamed:@"分钟"].CGImage;
            layer;
            
        });
    }
    return _minuteHand;
}

- (CALayer *)secondHand
{
    if (!_secondHand) {
        _secondHand = ({
            
            CALayer * layer = [CALayer layer];
            layer.frame = CGRectMake(0, 0, 4, 136);
            layer.position = CLOCK_CENTER;
            layer.anchorPoint = CGPointMake(0.5, 0.16);
            layer.contents = (__bridge id)[UIImage imageNamed:@"秒针"].CGImage;
            layer;
            
        });
    }
    return _secondHand;
}

- (CALayer *)question2Text
{
    if (!_question2Text) {
        _question2Text = ({
            
            CALayer * layer = [CALayer layer];
            layer.frame = CGRectMake(93, 350, 182, 58);
            layer.contents = (__bridge id)[UIImage imageNamed:@"夜间症状"].CGImage;
            layer;
            
        });
    }
    return _question2Text;
}

-(AnimationBlock)loadQuestion1:(BOOL)isEnter{
    
    AnimationBlock block;
    __weak typeof(self) weakSelf = self;
    CGFloat fromX;

    fromX = - 180.f;

    if (isEnter) {
        
        block = ^(){
            //表盘缩放
            //            weakSelf.question1Layer.position  =  CGPointMake(toX, 200);
            [self.layer addSublayer:self.question1Layer];
            DHCAnimation *animation =  [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(fromX) completionBlock:^(BOOL flag) {
                
            }];
//            animation.animation.beginTime = CACurrentMediaTime() + 1;
//            animation.animation.fillMode = kCAFillModeBoth;
            
            [weakSelf.question1Layer addAnimation:animation.animation forKey:nil];
            //
            
        };

        
    }else{
        block = ^(){
        
        DHCAnimation *animation =  [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(375.f/2) completionBlock:^(BOOL flag) {
            if (!isEnter) {
                [weakSelf.question1Layer removeFromSuperlayer];
            }
            
        }];
            weakSelf.question1Layer.position = CGPointMake(- 180, 180);
            
        
        [weakSelf.question1Layer addAnimation:animation.animation forKey:nil];

        
        
        };
        
    }
    
    return block;
    
    
}
-(AnimationBlock)loadQuestion2:(BOOL)isEnter{
    AnimationBlock block;
    
    __weak typeof(self) weakSelf = self;
          if (isEnter) {
            block = ^(){
                [self.layer addSublayer:self.question2Text];
                
                [self.layer addSublayer:self.clockFace];
                
                [self.layer addSublayer:self.hourHand];
                
                [self.layer addSublayer:self.minuteHand];
                
                [self.layer addSublayer:self.secondHand];
                
                DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:1.2 fromValue:@(375 + 100) completionBlock:nil];
                
                
                textAnimation.animation.beginTime = CACurrentMediaTime()+ 2;
                textAnimation.animation.fillMode = kCAFillModeBackwards;
                
                [weakSelf.question2Text addAnimation:textAnimation.animation forKey:nil];



              DHCAnimation *clockFaceScale = [DHCAnimation animationWithKeyPath:@"transform.scale" duration:0.8 fromValue:@(6.0) completionBlock:^(BOOL flag) {
                  
              }];
                clockFaceScale.animation.beginTime = CACurrentMediaTime() + 1;
                clockFaceScale.animation.fillMode = kCAFillModeBackwards;

              
                [weakSelf.clockFace addAnimation:clockFaceScale.animation forKey:nil];
                //时针动画 1.平移 2.旋转
                [weakSelf.hourHand addAnimation:[weakSelf translationandRotationFromPosition:CGPointMake(- 280, -280) toPosition:nil] forKey:nil];
                //分针动画
                [weakSelf.minuteHand addAnimation:[weakSelf translationandRotationFromPosition:CGPointMake(550, 540) toPosition:nil] forKey:nil];
                //秒针
                [weakSelf.secondHand addAnimation:[weakSelf translationandRotationFromPosition:CGPointMake(0, 567) toPosition:nil] forKey:nil];
                DHCAnimation *secondAnimation  = [DHCAnimation animationWithKeyPath:@"transform.rotation" duration:2 fromValue:@(-M_PI) completionBlock:nil];
                secondAnimation.animation.beginTime = CACurrentMediaTime()+1.8;
                secondAnimation.animation.repeatCount = 1;
                [weakSelf.secondHand addAnimation:secondAnimation.animation forKey:nil];
                //文件动画
                
//                textAnimation.animation.autoreverses = YES;
//                textAnimation.animation.repeatCount  =5;

              
            };
            
              
    }else{
        block = ^(){
            
            DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:1.2 fromValue:@(375.f/2) completionBlock:nil];
            textAnimation.animation.toValue = @(-100);
//            CGPoint position = weakSelf.question2Text.position;
//            position.x = -100;
//            [CATransaction setDisableActions:YES];
//            weakSelf.question2Text.position = position;
            [weakSelf.question2Text addAnimation:textAnimation.animation forKey:nil];

            DHCAnimation *animation =  [DHCAnimation animationWithKeyPath:@"transfrom.scale" duration:0.8 fromValue:@(0.8) completionBlock:^(BOOL flag) {
                if (!isEnter) {
                    [weakSelf.question2Text removeFromSuperlayer];
                    
                    [weakSelf.clockFace removeFromSuperlayer];
                    
                    [weakSelf.hourHand removeFromSuperlayer];
                    
                    [weakSelf.secondHand removeFromSuperlayer];
                    
                    [weakSelf.minuteHand removeFromSuperlayer];

                    
                }
                
                
                
            }];
            animation.animation.toValue = @(1.8);
            [weakSelf.clockFace addAnimation:animation.animation forKey:nil];
            //时针动画 1.平移 2.旋转
            [weakSelf.hourHand addAnimation:[weakSelf translationandRotationFromPosition:CLOCK_CENTER toPosition:[NSValue valueWithCGPoint:CGPointMake(-280, -280) ]] forKey:nil];
            //分针动画
            [weakSelf.minuteHand addAnimation:[weakSelf translationandRotationFromPosition: CLOCK_CENTER  toPosition:[NSValue valueWithCGPoint:CGPointMake(550, 540)]] forKey:nil];
            //秒针
            [weakSelf.secondHand addAnimation:[weakSelf translationandRotationFromPosition:CLOCK_CENTER toPosition:[NSValue valueWithCGPoint:CGPointMake(0, 567)]] forKey:nil];
            //文件动画
            
        
        };
        
    }
    return block;

  
}
-(AnimationBlock)loadQuestion3:(BOOL)isEnter{
    
    AnimationBlock block;
  
    __weak typeof (self)weakSelf = self;
    if (isEnter) {
        block = ^(){
            [weakSelf.layer addSublayer:weakSelf.question3Text];
            [weakSelf.layer addSublayer:weakSelf.cup];
            [weakSelf.layer addSublayer:weakSelf.water];
            [weakSelf addSubview:weakSelf.pill];
            
            DHCAnimation *cupAnimation = [DHCAnimation animationWithKeyPath:@"positon.x" duration:0.8 fromValue:@(375 + 100) completionBlock:^(BOOL flag) {
                
               [UIView animateWithDuration:6.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                   weakSelf.pill.frame  =  CGRectMake(160, 225, 45, 45);
               } completion:nil];
                DHCAnimation *waterAnimatiuon = [DHCAnimation animationWithKeyPath:@"opacity" duration:1 fromValue:@(0.1) completionBlock:nil];
                waterAnimatiuon.animation.beginTime = CACurrentMediaTime() + 4;
                waterAnimatiuon.animation.fillMode = kCAFillModeBackwards;
                [weakSelf.water addAnimation:waterAnimatiuon.animation forKey:nil];
                
                
            }];
//            cupAnimation.animation.beginTime = CACurrentMediaTime() + 3;
//            cupAnimation.animation.fillMode = kCAFillModeBoth;
            [weakSelf.cup addAnimation:cupAnimation.animation forKey:nil];
            [weakSelf.water addAnimation:cupAnimation.animation forKey:nil];
            
            
            
            DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(375 + 100) completionBlock:nil];
            
            textAnimation.animation.beginTime = CACurrentMediaTime() + 1;
            textAnimation.animation.fillMode = kCAFillModeBoth;
            [weakSelf.question3Text addAnimation:textAnimation.animation forKey:nil];
            
            
            
        };
    }else{
        
        
        block = ^(){
            
            DHCAnimation *animation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(182) completionBlock:^(BOOL flag) {
                
                if (!isEnter) {
                    [weakSelf.question3Text removeFromSuperlayer];
                    [weakSelf.cup removeFromSuperlayer];
                    [weakSelf.water removeFromSuperlayer];
                  
                    
                }
                
                
                
            }];
            animation.animation.toValue = @(- 100);
            [weakSelf.cup addAnimation:animation.animation forKey:nil];
            [weakSelf.water addAnimation:animation.animation forKey:nil];
            [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                weakSelf.pill.frame  =  CGRectMake(- 100, 263, 45, 45);
            } completion:nil];
            
        };
    
        DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(182) completionBlock:nil];
        textAnimation.animation.toValue = @(- 100);
        [weakSelf.question3Text addAnimation:textAnimation.animation forKey:nil];

        
            
            
        }

    return block;
    
}

-(AnimationBlock)loadQuestion4:(BOOL)isEnter{
    AnimationBlock block;
    __weak typeof (self) weakSelf = self;
    if (isEnter) {
        block = ^(){
            [self.layer addSublayer:self.cross];
            [self.layer addSublayer:self.question4Text];
            
            DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(375 + 100) completionBlock:^(BOOL flag) {
                
            }];
            textAnimation.animation.beginTime = CACurrentMediaTime() + 2;
            textAnimation.animation.fillMode = kCAFillModeBoth;
            [weakSelf.question4Text addAnimation:textAnimation.animation forKey:nil];
           
            
            
            DHCAnimation *crossxAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(375 + 100) completionBlock:^(BOOL flag) {
                DHCAnimation *crosszAniamtion = [DHCAnimation animationWithKeyPath:@"transform" duration:2 fromValue:[NSValue valueWithCATransform3D:self.cross.transform] completionBlock:^(BOOL flag) {
                    
                    
                    
                }];
                
                crosszAniamtion.animation.beginTime = CACurrentMediaTime() + 1;
                crosszAniamtion.animation.fillMode = kCAFillModeBackwards;
                
                self.cross.transform = CATransform3DRotate(self.cross.transform,  M_PI, 0, 1, 0);
                crosszAniamtion.animation.repeatCount = MAX_INPUT;
                crossxAnimation.animation.fillMode = kCAFillModeBoth;
                                [weakSelf.cross addAnimation:crosszAniamtion.animation forKey:nil];
                
            }];
            crossxAnimation.animation.beginTime = CACurrentMediaTime() + 2;
            crossxAnimation.animation.fillMode = kCAFillModeBackwards;

            [weakSelf.cross addAnimation:crossxAnimation.animation forKey:nil];

  };
    }else{
        
        
        block = ^(){
            
            
            DHCAnimation *textAnimation = [DHCAnimation animationWithKeyPath:@"position.x" duration:0.8 fromValue:@(180) completionBlock:^(BOOL flag) {
                [weakSelf.question4Text removeFromSuperlayer];
                [weakSelf.cross removeFromSuperlayer];
                
            }];
            textAnimation.animation.toValue = @(-120);
         
            [weakSelf.question4Text addAnimation:textAnimation.animation forKey:nil];
            [weakSelf.cross addAnimation:textAnimation.animation forKey:nil];

                        
        };
    }
    
    
    
    
    return block;
    
    
    
    
    
    
    
}

@end
